1
'****************************** Module Header ******************************\
2 ' Module Name: SharedSessionModule.vb
3 ' Project: VBASPNETShareSessionBetweenSubDomains
4 ' Copyright (c) Microsoft Corporation
6 ' This project demonstrates how to configure a SQL Server as SessionState and
7 ' make a module to share Session between two Web Sites with the same root domain.
9 ' SharedSessionModule is used to make Web Sites use the same Application Id and
10 ' Session Id to achieve sharing Session.
12 ' This source is subject to the Microsoft Public License.
13 ' See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
14 ' All other rights reserved.
16 '*****************************************************************************/
19 Imports System
.Configuration
20 Imports System
.Reflection
23 ''' A HttpModule used for sharing the session between Applications in
26 Public Class SharedSessionModule
27 Implements IHttpModule
28 ' Cache settings on memory.
29 Protected Shared applicationName
As String = ConfigurationManager
.AppSettings("ApplicationName")
30 Protected Shared rootDomain
As String = ConfigurationManager
.AppSettings("RootDomain")
32 #Region
"IHttpModule Members"
34 ''' Initializes a module and prepares it to handle requests.
36 ''' <param name="context">
37 ''' An System.Web.HttpApplication
38 ''' that provides access to the methods,
39 ''' properties, and events common to all application objects within
40 ''' an ASP.NET application.
42 Public Sub Init(ByVal context
As HttpApplication
) Implements IHttpModule
.Init
43 ' This module requires both Application Name and Root Domain to work.
44 If String.IsNullOrEmpty(applicationName
) OrElse
String.IsNullOrEmpty(rootDomain
) Then
48 ' Change the Application Name in runtime.
49 Dim runtimeInfo
As FieldInfo
= GetType(HttpRuntime
).GetField("_theRuntime", BindingFlags
.[Static] Or BindingFlags
.NonPublic
)
50 Dim theRuntime
As HttpRuntime
= DirectCast(runtimeInfo
.GetValue(Nothing), HttpRuntime
)
51 Dim appNameInfo
As FieldInfo
= GetType(HttpRuntime
).GetField("_appDomainAppId", BindingFlags
.Instance
Or BindingFlags
.NonPublic
)
53 appNameInfo
.SetValue(theRuntime
, applicationName
)
56 AddHandler context
.PostRequestHandlerExecute
, AddressOf context_PostRequestHandlerExecute
60 ''' Disposes of the resources (other than memory) used by the module
63 Public Sub Dispose() Implements IHttpModule
.Dispose
68 ''' Before sending response content to client, change the Cookie to Root Domain
69 ''' and store current Session Id.
71 ''' <param name="sender">
72 ''' An instance of System.Web.HttpApplication that provides access to
73 ''' the methods, properties, and events common to all application
74 ''' objects within an ASP.NET application.
76 Private Sub context_PostRequestHandlerExecute(ByVal sender
As Object, ByVal e
As EventArgs
)
77 Dim context
As HttpApplication
= DirectCast(sender
, HttpApplication
)
79 ' ASP.NET store a Session Id in cookie to specify current Session.
80 Dim cookie
As HttpCookie
= context
.Response
.Cookies("ASP.NET_SessionId")
82 If context
.Session IsNot
Nothing AndAlso
Not String.IsNullOrEmpty(context
.Session
.SessionID
) Then
83 ' Need to store current Session Id during every request.
84 cookie
.Value
= context
.Session
.SessionID
86 ' All Applications use one root domain to store this Cookie
87 ' So that it can be shared.
88 If rootDomain
<> "localhost" Then
89 cookie
.Domain
= rootDomain
92 ' All Virtual Applications and Folders share this Cookie too.